package javaBase.chapter02;

import java.util.Scanner;

public class TaxCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.print("请输入您的月薪（输入88退出程序）：");
            double monthlyIncome = scanner.nextDouble();

            if (monthlyIncome == 88) {
                System.out.println("退出程序");
                break;
            }

            double taxableIncome = monthlyIncome - 5000; // 起征点为5000元
            double tax = calculateTax(taxableIncome);

            System.out.println("应缴纳的税款为：" + tax + "元");
        }

        scanner.close();
    }

    public static double calculateTax(double taxableIncome) {
        double[] taxRates = {0.03, 0.1, 0.2, 0.25, 0.3, 0.35, 0.45}; // 适用税率
        double[] quickDeductions = {0, 105, 555, 1005, 2755, 5505, 13505}; // 速算扣除数

        double tax = 0;
        for (int i = 0; i < taxRates.length; i++) {
            if (taxableIncome <= 0) {
                break;
            }

            double rate = taxRates[i];
            double deduction = quickDeductions[i];

            if (taxableIncome <= deduction) {
                tax += taxableIncome * rate;
                break;
            } else {
                tax += deduction * rate;
                taxableIncome -= deduction;
            }
        }

        return tax;
    }
}
